<?php



	function get_index(){
		$str = <<<INDEX
<?php
	foreach(glob("../app/*.class.php") as \$file){
		require_once(\$file);
	}
	\$httppath = Config::getHTTPPath();
	\$httppath_array = array_filter(explode("/", \$httppath));

	// The path user is requesting
	\$path = \$_SERVER['REQUEST_URI'];
	\$path_array = array_filter(explode("/", \$path));
	\$final = array_diff_assoc(\$path_array, \$httppath_array);
	\$final = array_filter(\$final);

	// Get the first two elements
	\$i=1;
	foreach(\$final as \$element){
		if(\$i == 1) { 
			\$controller = \$element;
		}
		if(\$i == 2) {
			\$action = \$element;
		}
		\$i++;
	}

	\$controller = array_shift(\$final);
	\$action = array_shift(\$final);


	if(!isset(\$controller)) { \$controller="root"; }
	if(!isset(\$action)){ \$action = "index"; }

	\$object = ucfirst(\$controller)."_Controller";
	if(class_exists(\$object)){	
		\$control = new \$object(\$final);
		if(method_exists(\$control, \$action)){
			\$control->{\$action}(\$final);
			exit();	
		}else{
			\$control = new Error_Controller;
			\$control->index();
			exit();
		}
	} else {
		\$control = new Error_Controller;
		\$control->index();
		exit();
	}
?>
INDEX;
	return $str;
}

	function get_controller($cname, $appname, $actions=array()){
		
		$app_name = ucfirst(strtolower($appname));
		$controller_name_lc = strtolower($cname);
		$controller_name = ucfirst($controller_name_lc)."_Controller";		
		$str = <<<CONTROL
<?php
		
class $controller_name extends $app_name {
			
	private \$view_vars;
	private \$controller;

	public function index(){

		/*
		 * put all application code for the index action here...
		 */
		\$this->display(\$this->controller, __FUNCTION__, \$this->view_vars);
	}\n\n
CONTROL;

		if(isset($actions)) {
			// add all actions that the user requested
		}

		$str .= <<<CONTROL
	public function __construct(\$get_vars=array()){
		parent::construct(\$get_vars);
		\$this->controller = "$controller_name_lc";
	}

}

?>
CONTROL;
	
	return $str;
	
	}

	function get_main($appname){
		$app_name = ucfirst(strtolower($appname));
		$str = <<<M
<?php
	
class $app_name {
	
	// The Smarty Object
	protected \$view = null;

	// The PDO Object
	protected \$dbo = null;
	protected \$dbh = null;

	// The Error Messages
	public \$error = null;

	// The get variables
	protected \$get = null;
									
	// Default variables to assign to smarty
	protected \$root  = null;
	private \$project = null;

	function __construct(\$get_vars=array()){
		\$this->view = new {$app_name}_Smarty();
		\$this->root = Config::getRoot();
		\$this->view->assign('root', \$this->root);
		\$this->project = Config::getProjectName();	
		\$this->view->assign('project', \$this->project);
		if(isset(\$get_vars)){
			\$this->get = new {$app_name}_GET(\$get_vars);
		}
		
		// Get the PDO object
		\$this->dbo = new {$app_name}_DB();
		\$this->dbh = \$this->dbo->get_db_handle();	

	}

	/**
		* The default display method
		* @param controller name - null for none
		* @param action name - null for index
		* @param an array of values to pass to the template
		*/
	public function display(\$controller, \$action, \$assignments=null){
		if(isset(\$assignments)){
			foreach(\$assignments as \$key => \$value){
				\$this->view->assign(\$key, \$value);
			}
		}

		if(!isset(\$controller)){
			if(!isset(\$action)){
				\$this->view->display("index.tpl");
			} else {
				\$this->view->display("\$action.tpl");
			}
		} else {
			if(!isset(\$action)){
				\$this->view->display("\$controller/index.tpl");
			}else{
				\$this->view->display("\$controller/\$action.tpl");
			}
		}	
	}
	
	public function redirect(\$controller, \$action, \$path=""){
		\$c = null;
		\$a = null;

		if(!isset(\$controller)){
			header("Location: {\$this->root}");
		}else{
			\$c = \$controller;
			if(!isset(\$action)){
					header("Location: {\$this->root}/\$c");
				} else {
					header("Location: {\$this->root}/\$c/\$action/\$path");
				}
		}

	}
	
	public function getRoot(){
		return \$this->root;
	}
	
	

}

?>
M;
	return $str;
	}


?>
